package grpc

import (
	"context"
	"gitee.com/captials-team/ubdframe/src/common/utils"
	"gitee.com/captials-team/ubdframe/src/domain/configstc"
	"gitee.com/captials-team/ubdframe/src/domain/dto"
	"gitee.com/captials-team/ubdframe/src/domain/dto/respdata"
	v1log "gitee.com/captials-team/ubdframe/src/pkg/logs"
	pbService "gitee.com/captials-team/ubdframe/src/protobufs/pb/service"
	"go.uber.org/dig"
	"google.golang.org/grpc"
)

type UserService struct {
	l v1log.ILog

	conf *configstc.UserAppConfig
	cc   pbService.UserServiceClient
}

func NewUserService(di *dig.Container, l v1log.ILog, conf *configstc.UserAppConfig, cc grpc.ClientConnInterface) *UserService {
	ctr := &UserService{
		l:    l,
		conf: conf,
		cc:   pbService.NewUserServiceClient(cc),
	}

	return ctr
}

func (s *UserService) DoAuth(param *dto.AccountLoginParams) (*dto.AuthInfo, error) {
	if param.Account == "" || param.Password == "" {
		return nil, respdata.CParamsInvalid
	}

	resp, err := s.cc.DoAuth(context.Background(), utils.CopyTo(param, new(pbService.DoAuthRequest)))
	if err != nil {
		return nil, err
	}
	if resp.Code != 0 {
		return nil, respdata.New(resp.Code, resp.Message, resp.Data)
	}

	return &dto.AuthInfo{
		Id:       resp.Data.Id,
		Nickname: resp.Data.Nickname,
		Account:  resp.Data.Account,
		Status:   int(resp.Data.Status),
	}, nil
}

func (s *UserService) SignUp(param *dto.UserRegisterParams) (int64, error) {
	return 0, respdata.CNotSupport
}
